<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-11-11 15:35:21
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\Pro;


use App\Http\Services\Service;
use App\Repositories\ProProcessRouteDetailsRepository;
use App\Repositories\ProProcessRouteRepository;
use Illuminate\Support\Facades\DB;

/** 
 * @property ProProcessRouteRepository $repository 
 * @property ProProcessRouteDetailsRepository $details 
 * @author sunkaiyuan
 */
class ProcessRouteService extends Service
    {
    public function __construct(ProProcessRouteRepository $repository, ProProcessRouteDetailsRepository $details)
        {
        $this->repository = $repository;
        $this->details    = $details;
        }

    public function list()
        {
        // DB::enableQueryLog();
        $data = $this->repository->condition(request())->getGrid();
        // dd(DB::getQueryLog());
        return $data;
        }

    public function listWithPro()
        {
        return $this->repository->setRelation(['details' => ['process', 'bom', 'point_stick', 'material']])->whereStatus_id(0)->condition(request())->getList();
        }
    public function details()
        {
        return $this->details->condition(request())->orderBy("sort")->getGrid();
        }
    protected function save()
        {
        $data = request()->toArray();

        $data = $this->repository->autosave($data, true);
        $this->resetRoute($data);
        return $data;
        }
    private function resetRoute($data)
        {
        $front_process = null;

        $data->details->filter(function ($v) use (&$front_process)
            {
            if (isEmpty($v->rename)) {
                $v->rename = $v->process->name;
                $v->save();
                }
            //初始化
            $v->front_process_id = $v->next_process_id = 0;
            $v->next_route_id = $v->front_route_id = 0;
            if ($front_process) {
                $v->front_process_id            = $front_process->process_id;
                $v->front_route_id              = $front_process->id;
                $front_process->next_process_id = $v->process_id;
                $front_process->next_route_id   = $v->id;
                $front_process->save();
                }
            else
                $front_process = $v;

            $front_process = $v;

            });

        $front_process && $front_process->save();
        }
    protected function delete($request)
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            if ($v->status_id != 0) {
                fail("启用状态无法删除！");
                }
            $v->details->filter(function ($vv)
                {
                $vv->delete();
                });
            $v->delete();
            });
        }
    protected function audit()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            $this->resetRoute($v);
            enable($v, true);
            });
        }

    protected function noaudit()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            enable($v, false);
            });
        }
    }